<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    //1. 无参函数 function name(){ ... }
    function f1(){
        let a = 10;
        let b = 20;
        console.log(a+b)
    }
    // 调用函数
    f1();

    //2. 单参函数 function name(arg){ ... } 
    //3. 多参函数 function name(a1,a2,a3){...}
    // 带参数的函数，形参会存储至arguments数组中 
    function f2(a , b){
        // let sum = a+b;
        let sum = arguments[0] + arguments[1]
        console.log(sum)
    }
    // 调用函数
    f2(10,20)

    //3. 返回值函数 function name(){ return }
    function f3(a , b , c){
        let sum = a + b + c;
        return sum;
    }
    // 调用函数
    let s = f3("hello " , 10 , " 你好!"); 
    console.log(s)

    //4. 递归函数 function name(){ name() } 

    //5. 匿名函数 function(){}  标签事件 = function(){}
    let n = function(){
        return 20 + 22;
    }
    // 调用函数
    console.log(n());



    //1.求斐波那契数列Fibonacci中的第n个数是多少？      1 1 2 3 5 8 13 21...
      function fei(n){
        if(1==n||2==n){
            return 1;

        }else{
            return fei(n-1)+fei(n-2);
        }
    }
    console.log(`Fibonacci中第5个数是:${fei(5)}`);

    //2.输入一个年份，判断是否是闰年[闰年：能被4整数并且不能被100整数，或者能被400整数]
    function isLeapYear(year) {
        // 能被4整除但不能被100整除，或者能被400整除
        if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
            console.log(`${year} 年是闰年`);
            
        } else {
            console.log(`${year} 年是平年`);
        
        }
    }
    isLeapYear(2020); 
    isLeapYear(1900); 
    isLeapYear(2000); 

    //3.和电脑猜拳10次(电脑出拳用随机数)，显示每次结果，最后累计输赢平局结果
    function play(rounds = 10) {
    let choices = ['石头', '剪刀', '布'];
    let winCount = 0, loseCount = 0, tieCount = 0;
    for (let i = 1; i <= rounds; i++) {
        let player = prompt(`第 ${i} 局：请输入 “石头”、“剪刀” 或 “布”`);
        while (player!=choices[0]&&player!=choices[1]&&player!=choices[2]) {
            player = prompt(`输入有误，请重新输入 “石头”、“剪刀” 或 “布”`);
        }
        let comp = choices[Math.floor(Math.random() * 3)];
        let result;
        if (player === comp) {
            result = '平局';
            tieCount++;
        } else if (
            (player === '石头' && comp === '剪刀') ||
            (player === '剪刀' && comp === '布') ||
            (player === '布' && comp === '石头')
        ) {
            result = '你赢了';
            winCount++;
        } else {
            result = '你输了';
            loseCount++;
        }
        console.log(`第 ${i} 局：你出${player}，电脑出${comp},最后结果： ${result}`);
    }
    console.log(`你共赢了 ${winCount} 局，输了 ${loseCount} 局，平局 ${tieCount} 局`);
    }
    play();




</script>
</html>